<template>
  <div class="header-bar">
    <div class="header-left">
      <BreadCrumbs />
    </div>
    <div class="header-right">
      <div class="current-time">
        <el-icon><timer /></el-icon>
        <span>{{ currentTime }}</span>
      </div>
      <div class="header-actions">
        <el-tooltip content="返回前台" placement="bottom">
          <div class="action-btn" @click="goToFront">
            <el-icon><home-filled /></el-icon>
            <span>商城首页</span>
          </div>
        </el-tooltip>
        
        <div class="action-group">
          <el-tooltip content="刷新页面" placement="bottom">
            <div class="action-icon-btn" @click="refreshPage">
              <el-icon><refresh-right /></el-icon>
            </div>
          </el-tooltip>
          
          <el-tooltip :content="isFullscreen ? '退出全屏' : '全屏显示'" placement="bottom">
            <div class="action-icon-btn" @click="toggleFullScreen">
              <el-icon><component :is="isFullscreen ? 'close' : 'full-screen'" /></el-icon>
            </div>
          </el-tooltip>
        </div>

        <div class="divider"></div>
        <UserAvatar />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import BreadCrumbs from '../components/BreadCrumbs/index.vue'
import UserAvatar from '../components/UserAvatar/index.vue'

// 定义组件名称
defineOptions({
  name: 'HeaderBar'
})

// 响应式数据
const isFullscreen = ref(false)
const currentTime = ref('')
const timer = ref(null)
const router = useRouter()

// 刷新页面
const refreshPage = () => {
  window.location.reload()
}

// 切换全屏
const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen()
    isFullscreen.value = true
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen()
      isFullscreen.value = false
    }
  }
}

// 跳转到前台
const goToFront = () => {
  router.push('/')
}

// 更新时间
const updateTime = () => {
  const now = new Date()
  const options = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }
  currentTime.value = now.toLocaleDateString('zh-CN', options).replace(/\//g, '-')
}

// 生命周期钩子
onMounted(() => {
  document.addEventListener('fullscreenchange', () => {
    isFullscreen.value = !!document.fullscreenElement
  })
  updateTime()
  timer.value = setInterval(updateTime, 1000)
})

onBeforeUnmount(() => {
  if (timer.value) {
    clearInterval(timer.value)
  }
})
</script>

<style lang="less" scoped>
.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 16px;
  background: linear-gradient(to right, #ffffff, #f8faf5);
  border-bottom: 1px solid #ebeef5;
  
  .header-left {
    display: flex;
    align-items: center;
  }
  
  .header-right {
    display: flex;
    align-items: center;
    gap: 16px;
    
    .current-time {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #606266;
      font-size: 14px;
      background: #f5f7fa;
      padding: 6px 12px;
      border-radius: 20px;
      white-space: nowrap;
      
      :deep(.el-icon) {
        color: #67C23A;
        font-size: 16px;
      }
    }
    
    .header-actions {
      display: flex;
      align-items: center;
      gap: 12px;
      
      .action-btn {
        display: flex;
        align-items: center;
        gap: 6px;
        padding: 8px 16px;
        border-radius: 20px;
        cursor: pointer;
        transition: all 0.3s ease;
        background: linear-gradient(45deg, #67C23A, #85ce61);
        color: white;
        font-size: 14px;
        box-shadow: 0 2px 6px rgba(103, 194, 58, 0.2);
        
        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(103, 194, 58, 0.3);
        }
        
        :deep(.el-icon) {
          font-size: 16px;
        }
      }

      .action-group {
        display: flex;
        align-items: center;
        gap: 8px;
        background: #f5f7fa;
        padding: 4px;
        border-radius: 16px;
      }
      
      .action-icon-btn {
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        cursor: pointer;
        transition: all 0.3s ease;
        color: #606266;
        
        :deep(.el-icon) {
          font-size: 18px;
          transition: all 0.3s ease;
        }
        
        &:hover {
          background-color: white;
          color: #67C23A;
          box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
          
          :deep(.el-icon-refresh-right) {
            transform: rotate(180deg);
          }
        }
      }
      
      .divider {
        width: 1px;
        height: 24px;
        background: linear-gradient(to bottom, transparent, #e4e7ed, transparent);
        margin: 0 8px;
      }
    }
  }
}

:deep(.el-badge__content.is-fixed) {
  top: 8px;
  right: 8px;
  border: 2px solid white;
}

:deep(.el-badge__content) {
  background-color: #67C23A;
  box-shadow: 0 2px 4px rgba(103, 194, 58, 0.2);
}

:deep(.el-tooltip__popper) {
  font-size: 13px;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .current-time span {
    display: none;
  }
  
  .current-time {
    padding: 6px !important;
  }
}
</style>